//
//  CustomerRequest.swift
//  Employee
//
//  Created by tancheng on 2018/7/23.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON
import Moya


struct CustomerRequest: DataResultProtocol {
    static let shared = CustomerRequest()

    func getCustomerList(send: CustomerListSend, success: @escaping ([CustomerListModel],PageModel) -> (), failure: @escaping VoidClosure) -> Cancellable {
        
        //let requestKey = SJApp.functionKey()
        //if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
        let cancle = CustomerProvider.request(CustomerAPI.getCustomerList(send: send)) { (result) in
            
            //GlobalConfig.requestCache.removeValue(forKey: requestKey)
            
            self.dataProtocol(with: result, success: { (data) in
                let page = PageModel.init(json: data)
                let datas = CustomerListModel.array(from: data["elements"].arrayValue)
                success(datas,page)
                
            }, overdue: {
                let _ = CustomerRequest.shared.getCustomerList(send: send ,success: success, failure: failure)
                
            }, failure: failure)
            
        }
        //GlobalConfig.requestCache[requestKey] = cancle
        
        return cancle
        
        
    }
    
    
    func getCustomerDetail(send: CustomerDetailSend,success: @escaping (CustomerDetailModel) -> (), failure: @escaping VoidClosure) -> Cancellable {
        
        let cancle = CustomerProvider.request(CustomerAPI.getCustomerDetail(send: send)) { (result) in
            
            self.dataProtocol(with: result, success: { (data) in
                let model = CustomerDetailModel.init(json: data)
                success(model)
                
            }, overdue: {
                let _ = CustomerRequest.shared.getCustomerDetail(send: send ,success: success, failure: failure)
                
            }, failure: failure)
            
        }
       
        return cancle
        
    }
    
    func getTagTree(success: @escaping ([CustomerTagCategoryModel]) -> (), failure: @escaping VoidClosure) -> Cancellable {
        
        //let requestKey = SJApp.functionKey()
        //if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
        let cancle = CustomerProvider.request(CustomerAPI.getCustomerTagTree) { (result) in
            
            //GlobalConfig.requestCache.removeValue(forKey: requestKey)
            
            self.dataProtocol(with: result, success: { (data) in
                
                let datas = CustomerTagCategoryModel.array(from: data.arrayValue)
                success(datas)
                
            }, overdue: {
                let _ = CustomerRequest.shared.getTagTree(success: success, failure: failure)
                
            }, failure: failure)
            
        }
        //GlobalConfig.requestCache[requestKey] = cancle
        
        return cancle
        
        
    }
    
    func updateTags(send: CustomerTagSend,success: @escaping VoidClosure, failure: @escaping VoidClosure) -> Cancellable {
        
        let cancle = CustomerProvider.request(CustomerAPI.updateCustomerTags(send: send)) { (result) in
            
            self.dataProtocol(with: result, success: { (data) in
               
                success()
                
            }, overdue: {
                let _ = CustomerRequest.shared.updateTags(send: send ,success: success, failure: failure)
                
            }, failure: failure)
            
        }
        
        return cancle
        
    }

}
